package com.cyzy.controller;

import com.cyzy.dto.ResponseDto;
import com.cyzy.pojo.Coupon;
import com.cyzy.pojo.CouponSeckill;
import com.cyzy.pojo.UserCoupons;
import com.cyzy.service.CouponKillService;
import com.cyzy.service.CouponService;
import com.cyzy.service.UserCouponsService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/couponKill")
public class CouponKillController {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private CouponKillService couponKillService;

    @Autowired
    private CouponService couponService;

    @Autowired
    private UserCouponsService userCouponsService;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @GetMapping("/selectCouponKill")
    public ResponseDto selectCouponKill(){

        List<CouponSeckill> list = couponKillService.selectCouponKill();
        System.out.println(list+"----------list-------------");
        if(list==null){
            return ResponseDto.fail();
        }

        return ResponseDto.ok(list);
    }

    @PostMapping("/couponKill")
    public ResponseDto couponKill(@RequestBody CouponSeckill couponSeckill ,@RequestParam("userId")String userId){

        System.out.println(couponSeckill+"-------------------couponSeckill--------------------");
        System.out.println(couponSeckill.getSeckillId()+"------------------"+userId);

        //用户超买
        String setKey = "couponKillUser"+couponSeckill.getSeckillId();
        long add = redisTemplate.opsForSet().add(setKey, userId);
        System.out.println(add+"---------------add"+setKey);
        if(add==0){
            return ResponseDto.fail("请勿重复抢购！");
        }

        //平台超卖
        String couponKillKey = "seckill-"+couponSeckill.getSeckillId();
        //获取redis
        String s = redisTemplate.opsForList().rightPop(couponKillKey);
        System.out.println(s +"------------"+couponKillKey);
        if(s != null){

            //消息队列
            CouponSeckill couponSeckill1 = new CouponSeckill();
            couponSeckill1.setUserId(userId);
            couponSeckill1.setCouponId(couponSeckill.getCouponId());
            rocketMQTemplate.convertAndSend("coupon_kill", couponSeckill1);

//            Coupon coupon = couponService.selectCoupon(couponSeckill.getCouponId());
//            System.out.println(coupon+"----------coupon----------");
//            if(coupon == null){
//                //购买失败就移除用户
//                redisTemplate.opsForSet().remove(setKey,userId);
//                //再把redis优惠卷加回去
//                redisTemplate.opsForList().leftPush(couponKillKey,s);
//                return ResponseDto.fail("获取订单失败！");
//            }
//            //获取当前时间
//            Timestamp now = new Timestamp(System.currentTimeMillis());
//            // 计算毫秒数（1天=24*60*60*1000毫秒）
//            long millisecondsToAdd = coupon.getValidDays() * 24L * 60 * 60 * 1000;
//            Timestamp newTimestamp = new Timestamp(now.getTime() + millisecondsToAdd);
//            //生成订单
//            UserCoupons userCoupons = new UserCoupons();
//            userCoupons.setUserId(Long.parseLong(userId));
//            userCoupons.setTemplateId(couponSeckill.getCouponId());
//            userCoupons.setExpireTime(newTimestamp);
//
//            boolean save = userCouponsService.save(userCoupons);
//            if(!save){
//                //购买失败就移除用户
//                redisTemplate.opsForSet().remove(setKey,userId);
//                //再把redis优惠卷加回去
//                redisTemplate.opsForList().leftPush(couponKillKey,s);
//                return ResponseDto.fail("生成订单失败");
//            }

            return ResponseDto.ok("秒杀成功！");
        }else {
            //购买失败就移除
            redisTemplate.opsForSet().remove(setKey,userId);
            return ResponseDto.fail("秒杀失败！");
        }

    }

}
